/**
 * 随堂检测题目
 */
var localStorageManagers = require("script/localStorageManagers.js");
//mui初始化
mui.init({
	swipeBack: false
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	var self = plus.webview.currentWebview();
	document.getElementById('resname').innerHTML = self.resname;
	document.getElementById('fCatalogid').value=self.catalogid;
	document.getElementById('fCatalogname').value=self.catalogname;
	document.getElementById('fResname').value=self.resname;
	document.getElementById('fResid').value=self.resid;
	//加载随堂检测题目
	loadExamDiv();
	//切换题目
	mui('.top').on('tap', '.tihao1', clicktihao1); //已提交
	mui('.top').on('tap', '.tihao2', clicktihao2); //未提交
	//左右滑动切换题目
	window.addEventListener("swipeleft", swipeLeft);
	window.addEventListener("swiperight", swipeRight);
	
	//打开答题结果
	mui('header').on('tap', '#datijieguo', openDatijieguo);
	//选择答案时
	//mui('.mui-content').on('tap','input',tihaoChecked);
	//mui('.mui-content').on('longtap','input',tihaoChecked);
	//mui('.mui-content').on('click','input',tihaoChecked);
};
//序号设置选中样式--弃用
function tihaoChecked(){
	var isCheck=this.checked;
		//选择
		if(isCheck){
		//题号颜色
		var currtihaos=document.getElementsByClassName('tihao2 act');
		for(var i=0;i<currtihaos.length;i++){
			currtihaos[i].className='tihao tihao2 act akd';
		}
		//统一已答
		var akds=document.getElementsByClassName('akd');
		document.getElementById('sright').innerHTML=akds.length;
		}
		//取消选择
		if(this.type=='checkbox'&&!isCheck){
		var checkboxs=document.getElementsByName(this.name);
		var dai=0;
		for(var j=0;j<checkboxs.length;j++){
			var checkbox=checkboxs[j];
			if(checkbox.checked){
				dai++;
			}
		}
		if(dai<=1){
			var currtihaos=document.getElementsByClassName('tihao tihao2 act');
		for(var i=0;i<currtihaos.length;i++){
			currtihaos[i].className='tihao tihao2 act';
		}
		//统一已答
		var akds=document.getElementsByClassName('akd');
		document.getElementById('sright').innerHTML=akds.length;
		}
		}
}
//加载随堂检测题目
function loadExamDiv() {
	ajaxExamList();
}
//ajax加载随堂检测题目
function ajaxExamList() {
	var resid = plus.webview.currentWebview().resid;
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + "/mobile/classroomtest/getExamQuestion.do";
	mui.ajax(url, {
		data: {
			userid: userid,
			resid: resid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data == null) return;
			buildExamDiv(data);
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//buildExamDiv
function buildExamDiv(data) {
	var top1div = document.getElementById('top1');
	var elist1div = document.getElementById('elist1');
	var top2div = document.getElementById('top2');
	var elist2div = document.getElementById('elist2');
	var isSubmit = data.isSubmit; //是否提交
	var questions = data.questions;
	document.getElementById('stotal').innerHTML = '/' + questions.length;
	for (var i = 0; i < questions.length; i++) {
		var ques = questions[i];
		var type = '单选';
		if (ques.quesType == 2) type = '多选';
		if (ques.quesType == 3) type = '判断';
		//设置题号
		var act = '';
		if (i == 0) act = 'act';
		var topitem = '<span class="tihao tihao2 ' + act + '" tihao="' + ques.quesNo + '" id="tihao_' + ques.quesNo + '">' + ques.quesNo + '</span>';
		top2div.innerHTML = top2div.innerHTML + topitem;
		//添加题目
		var timuitem = '<div class="exam exam2 ' + act + '" id="exam2_' + (ques.quesNo) + '" quesid="'+ques.quesId+'" rightanswer="'+ques.quesAnswer+'" quesno="'+ques.quesNo+'">';
		timuitem += '<div class="examtype">【' + type + '】</div>';
		timuitem += '<div class="examcontent">' + ques.quesTitle + '</div>';
		timuitem += '<div class="xxs">';
		timuitem += getXuanxiang(ques);
		timuitem += '</div>';
		timuitem += '</div>';
		elist2div.innerHTML = elist2div.innerHTML + timuitem;
	}
};
//选项拼接
function getXuanxiang(ques){
	var xuanxiang='';
	var inputtype='radio';
	if(ques.quesType==2)inputtype='checkbox';
	if (ques.optionContent1.length > 0) {
		    var value='A';
		    if(ques.quesType==3)value=1;
		    var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="'+value+'" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent1 + '</span></div>';
		}
		if (ques.optionContent2.length > 0) {
			var value='B';
			 if(ques.quesType==3)value=0;
			var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="'+value+'" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent2 + '</span></div>';
		}
		if (ques.optionContent3.length > 0) {
			var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="C" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent3 + '</span></div>';
		}
		if (ques.optionContent4.length > 0) {
			var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="D" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent4 + '</span></div>';
		}
		if (ques.optionContent5.length > 0) {
			var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="E" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent5 + '</span></div>';
		}
		if (ques.optionContent6.length > 0) {
			var checkinput='<span class="mui-'+inputtype+'"><label></label><input name="checkbox'+ques.quesNo+'" value="F" type="'+inputtype+'"></span>';
			xuanxiang += '<div class="xx"><span class="c2">' + checkinput + ques.optionContent6 + '</span></div>';
		}
		return xuanxiang;
};
//切换题目
//已提交的题目
function clicktihao1() {
	//题号按钮颜色改变
	var tihaos = document.getElementsByClassName('tihao1');
	for (var i = 0; i < tihaos.length; i++) {
		tihaos[i].className = 'tihao tihao1';
	}
	this.className = 'tihao tihao1 act';
	//显示对应题目
	var exams = document.getElementsByClassName('exam1');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam1';
	}
	var tihao = this.getAttribute('tihao');
	var timuDiv = document.getElementById('exam1_' + tihao);
	timuDiv.className = 'exam exam1 act';
};
//未提交的题目
function clicktihao2() {
	settihaocheck();
	//题号按钮颜色改变
	var tihaos = document.getElementsByClassName('tihao2');
	for (var i = 0; i < tihaos.length; i++) {
		var classname=tihaos[i].className;
		classname=classname.replace('act','');
		tihaos[i].className = classname;
	}
	this.className = this.className+' act';
	//显示对应题目
	var exams = document.getElementsByClassName('exam2');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam2';
	}
	var tihao = this.getAttribute('tihao');
	var timuDiv = document.getElementById('exam2_' + tihao);
	timuDiv.className = 'exam exam2 act';
	
};
function settihaocheck(){
	var exams=document.getElementsByClassName('exam');
	var isnum=0;
	for(var i=0;i<exams.length;i++){
		var exam=exams[i];
		var quesno=exam.getAttribute('quesno');
		var checkboxs=document.getElementsByName('checkbox'+quesno);
		var isyida=false;
		for(var j=0;j<checkboxs.length;j++){
			var checkbox=checkboxs[j];
			if(checkbox.checked){
				isyida=true;
				break;
			}
		}
		var tihaoEle=document.getElementById('tihao_'+quesno);
		var classname=tihaoEle.className;
		if(isyida){
			tihaoEle.className=classname+' akd';
			isnum++;
		}else{
			tihaoEle.className=classname.replace('akd','');
		}
	}
	document.getElementById('sright').innerHTML=isnum;
}
//打开答题结果---直接提交
function openDatijieguo(){
	//做了哪几道题
	var catalogid=document.getElementById('fCatalogid').value;
	var catalogname=document.getElementById('fCatalogname').value;
	var resid=document.getElementById('fResid').value;
	var resname=document.getElementById('fResname').value;
	var exams=document.getElementsByClassName('exam');
	var daanArr=new Array();
	var datistr='';
	var weidanum=0;
	for(var i=0;i<exams.length;i++){
		var exam=exams[i];
		var quesid=exam.getAttribute('quesid');
		var rightanswer=exam.getAttribute('rightanswer');
		var quesno=exam.getAttribute('quesno');
		var checkboxs=document.getElementsByName('checkbox'+quesno);
		var daans='';
		var isyida=false;
		for(var j=0;j<checkboxs.length;j++){
			var checkbox=checkboxs[j];
			if(checkbox.checked){
				daans+=checkbox.value+',';
				isyida=true;
			}
		}
		if(daans.length>0)daans=daans.substring(0,daans.length-1);
		var data={
				"resourceId":resid,
				"questionId":quesid,
				"replyAnswer":daans,
				"rightAnswer":rightanswer
		};
		daanArr.push(data);
		if(isyida){
			datistr+='1,';
		}else{
			datistr+='0,';
			weidanum++;
		}
	}
	if(datistr.length>0)datistr=datistr.substring(0,datistr.length-1);
	var daansStr=JSON.stringify(daanArr);
	var testnum=exams.length;
	var data={
		catalogid:catalogid,
		catalogname:catalogname,
		resid:resid,
		resname:resname,
		testnum:testnum,
		datistr:datistr,
		daansStr:daansStr
	};
	submittest(daansStr,weidanum);
	//直接提交
	//mui.openWindow({
	//	url:'testresult.html',
	//	id:'selfcenter/classroomtest/testresult',
	//	extras:data
	//});
};
//提交随堂检测
function submittest(daansStr,weidanum) {
	
	if (weidanum > 0) {
		var issubmit = confirm('您还有' + weidanum + '道题未做,确定要提交吗?');
		if (!issubmit) return;
	}else{
		var issubmit = confirm('您确定要提交当前随堂检测吗?');
		if (!issubmit) return;
	}
	
	var userid = localStorageManagers.getUseridSession();
	//var daansStr = plus.webview.currentWebview().daansStr;
	var url = serverAjax + "/mobile/classroomtest/submitExanAnswer.do";
	mui.ajax(url, {
		type: 'post',
		data: {
			resid:document.getElementById('fResid').value,
			userid: userid,
			answers: daansStr
		},
		dataType: "json",
		timeout: 10000,
		success: function(data) {
		   mui.toast('提交随堂检测成功');
		   var parentWebView = plus.webview.getWebviewById("selfcenter/classroomtest/testindex");
	       parentWebView.evalJS("loadNoteDiv()");
		   mui.back();
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});
};
//左滑切换题目
function swipeLeft(){
	var tihaos = document.getElementsByClassName('tihao2');
	var nextTihao=null;
	for (var i = 0; i < tihaos.length-1; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i+1];
		var classname=tihaos[i].className;
		classname=classname.replace('act','');
		tihaos[i].className = classname;
	}
	if(nextTihao!=null){
		nextTihao.className = nextTihao.className+' act';
	   //显示对应题目
	var exams = document.getElementsByClassName('exam2');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam2';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('exam2_' + tihao);
	timuDiv.className = 'exam exam2 act';
	}
	settihaocheck();
};
//右滑切换题目
function swipeRight(){
	var tihaos = document.getElementsByClassName('tihao2');
	var nextTihao=null;
	for (var i = 1; i < tihaos.length; i++) {
		if(tihaos[i].className.indexOf('act')!=-1)
		nextTihao=tihaos[i-1];
		var classname=tihaos[i].className;
		classname=classname.replace('act','');
		tihaos[i].className = classname;
	}
	if(nextTihao!=null){
	   nextTihao.className = nextTihao.className+' act';
	    //显示对应题目
	var exams = document.getElementsByClassName('exam2');
	for (var i = 0; i < exams.length; i++) {
		exams[i].className = 'exam exam2';
	}
	var tihao = nextTihao.getAttribute('tihao');
	var timuDiv = document.getElementById('exam2_' + tihao);
	timuDiv.className = 'exam exam2 act';
	}
	settihaocheck();
};